$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/tbuser/list',
        datatype: "json",
        colModel: [			
			{ label: 'userId', name: 'userId', index: 'user_id', width: 50, key: true },
			{ label: '用户名', name: 'username', index: 'username', width: 80 }, 			
			{ label: '手机号', name: 'mobile', index: 'mobile', width: 80 }, 			
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 },
			{ label: '邮箱', name: 'email', index: 'email', width: 80 },
			{ label: '昵称', name: 'nickname', index: 'nickname', width: 80 },
			{ label: '头像', name: 'profileimageurl', index: 'profileImageUrl', width: 80},
			{ label: '性别', name: 'sex', index: 'sex', width: 80,formatter: function(value, options, row){
                return value === false ?
                    '<span class="label label-danger">女</span>' :
                    '<span class="label label-success">男</span>';
            } },
			//0：普通用户 1：会员 2：代理
			{ label: '用户类型', name: 'type', index: 'type', width: 80,formatter: function (value, options, row) {
                if(value === 0){
                    return '<span class="label label-danger">白户</span>';
                }
                if(value === 1){
                    return '<span class="label label-success">会员</span>';
                }
        		if(value === 2){
                    return '<span class="label label-primary">代理</span>';
                }
            }},
			// { label: '魅力值', name: 'charmvalue', index: 'charmvalue', width: 80 },
			// { label: '能量值', name: 'energyvalue', index: 'energyvalue', width: 80 },
			// { label: '代理库存', name: 'agentinventory', index: 'agentinventory', width: 80 },
			// { label: '积分等级', name: 'integrallevel', index: 'integrallevel', width: 80 },
			// { label: '代理等级', name: 'agentlevel', index: 'agentlevel', width: 80 },
			// { label: '能量值等级', name: 'energyvaluelevel', index: 'energyvaluelevel', width: 80 },
			// { label: '佣金余额', name: 'balancecommission', index: 'balanceCommission', width: 80 },
			// { label: '余额', name: 'balance', index: 'balance', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.records",
            page: "page.current",
            total: "page.pages",
            records: "page.total"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
            like: null
        },
		showList: true,
        teamList: false,
        showList2: false,
		title: null,
		title2:null,
		tbuser: {},
	},
	methods: {
        team1: function () {
            var userId = getSelectedRow();
            if(userId == null){
                return ;
            }
            vm.showList = false;
            vm.showList2 = false;
            vm.teamList = true;
            vm.title2 = "一级团队";
            vm.getTeam1(userId);
        },
        team2: function () {
            var userId = getSelectedRow();
            if(userId == null){
                return ;
            }
            vm.showList = false;
            vm.showList2 = false;
            vm.teamList = true;
            vm.title2 = "二级团队";
            vm.getTeam2(userId);
        },
        team3: function () {
            var userId = getSelectedRow();
            if(userId == null){
                return ;
            }
            vm.showList = false;
            vm.showList2 = false;
            vm.teamList = true;
            vm.title2 = "三级团队";
            vm.getTeam3(userId);
        },
        team4: function () {
            var userId = getSelectedRow();
            if(userId == null){
                return ;
            }
            vm.showList = false;
            vm.showList2 = false;
            vm.teamList = true;
            vm.title2 = "四级团队";
            vm.getTeam4(userId);
        },
		getTeam1:function (userId) {
            $("#teamListApp").jqGrid({
                url: baseURL + 'sys/tbuser/team1',
                postData:{'parentid': userId},
                datatype: "json",
                colModel: [
                    { label: 'userId', name: 'userId', index: 'user_id', width: 50, key: true },
                    { label: '用户名', name: 'username', index: 'username', width: 80 },
                    { label: '手机号', name: 'mobile', index: 'mobile', width: 80 },
                    { label: '创建时间', name: 'createTime', index: 'create_time', width: 80 },
                    { label: '邮箱', name: 'email', index: 'email', width: 80 },
                    { label: '昵称', name: 'nickname', index: 'nickname', width: 80 },
                    { label: '头像', name: 'profileimageurl', index: 'profileImageUrl', width: 80},
                    { label: '性别', name: 'sex', index: 'sex', width: 80,formatter: function(value, options, row){
                        return value === false ?
                            '<span class="label label-danger">女</span>' :
                            '<span class="label label-success">男</span>';
                    } },
                    //0：普通用户 1：会员 2：代理
                    { label: '用户类型', name: 'type', index: 'type', width: 80,formatter: function (value, options, row) {
                        if(value === 0){
                            return '<span class="label label-danger">白户</span>';
                        }
                        if(value === 1){
                            return '<span class="label label-success">会员</span>';
                        }
                        if(value === 2){
                            return '<span class="label label-primary">代理</span>';
                        }
                    }},
                ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth:true,
                multiselect: true,
                pager: "#teamListAppPager",
                jsonReader : {
                    root: "page.records",
                    page: "page.current",
                    total: "page.pages",
                    records: "page.total"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    $("#teamListApp").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });
        },
        getTeam2:function (userId) {
            $("#teamListApp").jqGrid({
                url: baseURL + 'sys/tbuser/team2',
                postData:{'parentid2': userId},
                datatype: "json",
                colModel: [
                    { label: 'userId', name: 'userId', index: 'user_id', width: 50, key: true },
                    { label: '用户名', name: 'username', index: 'username', width: 80 },
                    { label: '手机号', name: 'mobile', index: 'mobile', width: 80 },
                    { label: '创建时间', name: 'createTime', index: 'create_time', width: 80 },
                    { label: '邮箱', name: 'email', index: 'email', width: 80 },
                    { label: '昵称', name: 'nickname', index: 'nickname', width: 80 },
                    { label: '头像', name: 'profileimageurl', index: 'profileImageUrl', width: 80},
                    { label: '性别', name: 'sex', index: 'sex', width: 80,formatter: function(value, options, row){
                        return value === false ?
                            '<span class="label label-danger">女</span>' :
                            '<span class="label label-success">男</span>';
                    } },
                    //0：普通用户 1：会员 2：代理
                    { label: '用户类型', name: 'type', index: 'type', width: 80,formatter: function (value, options, row) {
                        if(value === 0){
                            return '<span class="label label-danger">白户</span>';
                        }
                        if(value === 1){
                            return '<span class="label label-success">会员</span>';
                        }
                        if(value === 2){
                            return '<span class="label label-primary">代理</span>';
                        }
                    }},
                ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth:true,
                multiselect: true,
                pager: "#teamListAppPager",
                jsonReader : {
                    root: "page.records",
                    page: "page.current",
                    total: "page.pages",
                    records: "page.total"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    $("#teamListApp").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });
        },
        getTeam3:function (userId) {
            $("#teamListApp").jqGrid({
                url: baseURL + 'sys/tbuser/team3',
                postData:{'parentid3': userId},
                datatype: "json",
                colModel: [
                    { label: 'userId', name: 'userId', index: 'user_id', width: 50, key: true },
                    { label: '用户名', name: 'username', index: 'username', width: 80 },
                    { label: '手机号', name: 'mobile', index: 'mobile', width: 80 },
                    { label: '创建时间', name: 'createTime', index: 'create_time', width: 80 },
                    { label: '邮箱', name: 'email', index: 'email', width: 80 },
                    { label: '昵称', name: 'nickname', index: 'nickname', width: 80 },
                    { label: '头像', name: 'profileimageurl', index: 'profileImageUrl', width: 80},
                    { label: '性别', name: 'sex', index: 'sex', width: 80,formatter: function(value, options, row){
                        return value === false ?
                            '<span class="label label-danger">女</span>' :
                            '<span class="label label-success">男</span>';
                    } },
                    //0：普通用户 1：会员 2：代理
                    { label: '用户类型', name: 'type', index: 'type', width: 80,formatter: function (value, options, row) {
                        if(value === 0){
                            return '<span class="label label-danger">白户</span>';
                        }
                        if(value === 1){
                            return '<span class="label label-success">会员</span>';
                        }
                        if(value === 2){
                            return '<span class="label label-primary">代理</span>';
                        }
                    }},
                ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth:true,
                multiselect: true,
                pager: "#teamListAppPager",
                jsonReader : {
                    root: "page.records",
                    page: "page.current",
                    total: "page.pages",
                    records: "page.total"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    $("#teamListApp").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });
        },
        getTeam4:function (userId) {
            $("#teamListApp").jqGrid({
                url: baseURL + 'sys/tbuser/team4',
                postData:{'parentid4': userId},
                datatype: "json",
                colModel: [
                    { label: 'userId', name: 'userId', index: 'user_id', width: 50, key: true },
                    { label: '用户名', name: 'username', index: 'username', width: 80 },
                    { label: '手机号', name: 'mobile', index: 'mobile', width: 80 },
                    { label: '创建时间', name: 'createTime', index: 'create_time', width: 80 },
                    { label: '邮箱', name: 'email', index: 'email', width: 80 },
                    { label: '昵称', name: 'nickname', index: 'nickname', width: 80 },
                    { label: '头像', name: 'profileimageurl', index: 'profileImageUrl', width: 80},
                    { label: '性别', name: 'sex', index: 'sex', width: 80,formatter: function(value, options, row){
                        return value === false ?
                            '<span class="label label-danger">女</span>' :
                            '<span class="label label-success">男</span>';
                    } },
                    //0：普通用户 1：会员 2：代理
                    { label: '用户类型', name: 'type', index: 'type', width: 80,formatter: function (value, options, row) {
                        if(value === 0){
                            return '<span class="label label-danger">白户</span>';
                        }
                        if(value === 1){
                            return '<span class="label label-success">会员</span>';
                        }
                        if(value === 2){
                            return '<span class="label label-primary">代理</span>';
                        }
                    }},
                ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth:true,
                multiselect: true,
                pager: "#teamListAppPager",
                jsonReader : {
                    root: "page.records",
                    page: "page.current",
                    total: "page.pages",
                    records: "page.total"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    $("#teamListApp").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });
        },
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.tbuser = {};
		},
		update: function (event) {
			var userId = getSelectedRow();
			if(userId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(userId)
		},
		saveOrUpdate: function (event) {
			var url = vm.user.userId == null ? "sys/tbuser/save" : "sys/tbuser/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.user),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var userIds = getSelectedRows();
			if(userIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/tbuser/delete",
				    contentType: "application/json",
				    data: JSON.stringify(userIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
        back() {
            history.go(0)
        },
        info: function(event){
            var userId = getSelectedRow();
            if(userId == null){
                return ;
            }
          	vm.showList = false;
            vm.showList2 = true;
            vm.teamList = false;
            vm.title = "查看";
            vm.getInfo(userId)
		},
		getInfo: function(userId){
			$.get(baseURL + "sys/tbuser/info/"+userId, function(r){
                vm.tbuser = r.tbUser;
            });
		},
		reload: function (event) {
            vm.showList = true;
            vm.showList2 = false;
            vm.teamList = false;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'like': vm.q.like},
                page:page
            }).trigger("reloadGrid");
		}
	}
});